﻿StaffInput = Class.create();

StaffInput.prototype = Object.extend(new Common(), {
    initialize: function () {
        this.initCommonProperty();

        this.staff_id = "";

        this.callback = null;

        this.position_id = "";
    },
    init: function () {
        //保存处理
        Event.observe($(this.dom.btnSave), 'click', this.onClick_Save.bind(this));

        //删除处理
        Event.observe($(this.dom.btnDelete), 'click', this.onClick_Delete.bind(this));

        //清空处理
        Event.observe($(this.dom.btnClear), 'click', this.onClick_Clear.bind(this));

        //关闭处理
        Event.observe($(this.dom.btnClose), 'click', this.onClick_Close.bind(this));

        this.staff_id = $(this.dom.staff_id).value;

        //画面加载时职位取得
        this.getPositionList();
    },
    //画面加载时职位取得
    getPositionList: function () {
        this.sendAjaxByModel("/Master/GetPositionList", null, this.getPositionListCallback.bind(this));
    },
    //职位CallBack
    getPositionListCallback: function (response) {
        var json = this.jsonParse(response);
        $j("#" + this.dom.position_id).append("<option value=''>--请选择职位--</option>");
        for (var i = 0; i < json.length; i++) {
            $j("#" + this.dom.position_id).append("<option value='" + json[i].position_id + "'>" + json[i].position_name + "</option>");
        }
    },
    //关闭按钮处理
    onClick_Close: function () {
        CmnDialog.hide($("addStaffInputFrame"));
    },
    onClick_Save: function () {
        CmnMessage.showMessage("是否确定提交处理？", MSG_TYPE_CONFIRM, this.saveData.bind(this));
    },
    saveData: function () {
        this.inputModel = this.getModelByCssClass(this.inputModel, "", this.dom.clientID);
        this.sendAjaxByModel("/Master/SaveStaffInfo", this.inputModel, this.saveDataCallback.bind(this));
    },
    saveDataCallback: function (response) {
        var json = this.jsonParse(response);
        if (json.result > 0) {
            CmnMessage.showMessage(json.message, MSG_TYPE_INFO, this.messageCallBack.bind(this));
        }
        else {
            CmnMessage.showMessage(json.message, MSG_TYPE_ERROR);
        }
    },
    //清空处理
    onClick_Clear: function () {
        this.clearForm(this.dom.clientID);
    },
    //页面初期化
    initForm: function () {
        //清空页面数据
        this.clearForm(this.dom.clientID);

        //判断页面是编辑还是新规
        if (this.staff_id == "" || this.staff_id == undefined) {
            $j("#" + this.dom.btnDelete).css("display", "none");
            $(this.dom.staff_id).value = 0;
        }
        else {
            this.getStaffInfoById();
            $j("#" + this.dom.btnDelete).css("display", "");
        }
        $j("#codeMessage").text("");
    },
    //show出画面
    show: function (callback, staff_id) {
        $("addStaffInputFrame").style.display = '';
        //show出画面
        CmnDialog.show($("addStaffInputFrame"), "新规员工", true);

        this.staff_id = staff_id;

        //初始化页面控件
        this.initForm();

        this.callback = callback;
    },
    //画面弹出Message框点击确定或取消后处理
    messageCallBack: function () {
        CmnDialog.hide($("addStaffInputFrame"));

        this.position_id = $(this.dom.position_id).value;

        if (typeof this.callback == "function") {
            this.callback(this.position_id);
        }
    },
    getStaffInfoById: function () {
        var data = {};
        data.staff_id = this.staff_id;
        this.sendAjaxByModel("/Master/GetStaffInfoById", data, this.getStaffInfoByIdCallback.bind(this));
    },

    getStaffInfoByIdCallback: function (response) {
        var json = this.jsonParse(response);
        if (json != null) {
            //把json的值赋给画面
            this.initInputForm(json[0], this.dom.clientID);

            //给position_id赋值，以便于在保存成功后刷新position_id对应的页面信息
            this.position_id = json[0].position_id;
        }
    },
    onClick_Delete: function () {
        CmnMessage.showMessage("确定删除此信息？", MSG_TYPE_CONFIRM, this.deleteData.bind(this));
    },
    //删除处理
    deleteData: function () {
        var data = [];
        data.push(this.staff_id);
        this.sendAjaxByList("/Master/DeleteStaffInfo", data, this.onClickDeleteCallback.bind(this));
    },
    //删除CallBack
    onClickDeleteCallback: function (response) {
        var json = this.jsonParse(response);
        if (json.result > 0) {
            CmnMessage.showMessage("删除成功.", MSG_TYPE_INFO, this.messageCallBack.bind(this));
        }
        else {
            CmnMessage.showMessage("删除失败！", MSG_TYPE_ERROR);
        }
    }
});